package com.kingsoft.demo.xml.dom4j;

import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * 读取xml文件方法4
 * @author Administrator
 *
 */
public class TestReader4 {
	
	/**
	 * 使用XPath语法来直接定位到某个节点
	 * @throws Exception
	 */
	public  void readXml() throws Exception{
		
		 SAXReader sr=new SAXReader();		 
		 Document doc=sr.read("src/emp.xml");
		 
		 List list=doc.selectNodes("ROWDATA/ROW/EMPNO"); //直接取出所有的ename
	
		 for(Object obj:list)
		 {
			 Element el=(Element) obj;
			 System.out.println(el.getText());
		 }	
	}
	/**
	 * 使用XPath语法直接定位到某个属性
	 * @throws Exception
	 */
	public  void readAttribute() throws Exception{
		
		 SAXReader sr=new SAXReader();		 
		 Document doc=sr.read("src/emp.xml");
	
		//读取属性：
		 List list=doc.selectNodes("ROWDATA/ROW/ENAME/@firstname");
		 for(Object obj:list)
		 {
			 Attribute el=(Attribute) obj;
			 System.out.println(el.getText());
		 }
	
	}
	public static void main(String[] args) {
		TestReader4 tr=new TestReader4();
		try{
			tr.readAttribute();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}
